package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ImportExcelInterface;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.common.dto.HeaderInfoDto;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class PjMeterImport implements ImportExcelInterface {

    @Override
    public MessageBean<?> importExel(SessionFactory factory, UserInfoBean userInfo, RequestBean req,
                                     List<Map<String, String>> data, MultipartFile file) {
        try {
            List<PjMeter> pjMeterList = new ArrayList<>();
            // setupMeterAddr不能相同
            if (data.size() >= 2) {
                for (int i = 0; i < data.size()-1; i++) {
                    String setupMeterAddr1 = data.get(i).get("setupMeterAddr");
                    for (int j = i + 1; j < data.size(); j++) {
                        String setupMeterAddr2 = data.get(j).get("setupMeterAddr");
                        if (Objects.equals(setupMeterAddr1, setupMeterAddr2)) {
                            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "第" + (i + 1) + "行与" + (j + 1) + "行地址相同", null);
                        }
                    }
                }
            }
            // 1. 循环获取数据
            for (Map<String, String> map : data) {
                PjMeter pjMeter = new PjMeter();
                UserBean userBean = new UserBean();

                // 1.1 setupMeterAddr
                userBean.setSetupMeterAddr(map.get("setupMeterAddr"));
                // 1.2 meterSite
                userBean.setMeterSite(map.get("meterSite"));
                // 1.3 meterBjms
                String meterBjmsName = map.get("meterBjmsName");
                String meterBjms = Tools.getDicValueByCodeAndName(factory, "BJMS",meterBjmsName);
                if (StringUtils.isEmpty(meterBjms)) {
                    return MessageBean.create(Constant.MESSAGE_INT_FAIL, "请输入正确的表井模式", null);
                }
                userBean.setMeterBjms(meterBjms);
                // 1.4 meterBjxs
                String meterBjxsName = map.get("meterBjxsName");
                String meterBjxs = Tools.getDicValueByCodeAndName(factory, "BJXS",meterBjxsName);
                if (StringUtils.isEmpty(meterBjxs)) {
                    return MessageBean.create(Constant.MESSAGE_INT_FAIL, "请输入正确的表井形式", null);
                }
                userBean.setMeterBjxs(meterBjxs);
                pjMeter.setUserInfo(userBean);
                pjMeter.setMsInfo(new MsInfoDto());
                pjMeterList.add(pjMeter);
            }
            // 2. 返回数据
            MessageBean<List> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "工程报装-水表信息导入成功", List.class);
            msg.setData(pjMeterList);

            return msg;
        } catch (Exception e) {
            factory.close(false);
            logger.error("工程报装-水表导入失败",e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "工程报装-水表导入失败", null);
        }
    }

    @Override
    public List<HeaderInfoDto> getHeaderInfo(UserInfoBean userInfo) {
        List<HeaderInfoDto> headerInfos = new ArrayList<HeaderInfoDto>();

        headerInfos.add(new HeaderInfoDto("装表地址", false, "setupMeterAddr"));
        headerInfos.add(new HeaderInfoDto("表位", false, "meterSite"));
        headerInfos.add(new HeaderInfoDto("表井模式", false, "meterBjmsName"));
        headerInfos.add(new HeaderInfoDto("表井形式", false, "meterBjxsName"));

        return headerInfos;
    }

    @Override
    public boolean isCheckHeader() {
        // TODO Auto-generated method stub
        return true;
    }
}
